package tq.com.tqandroidimdemon;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONObject;

import tq.com.tqandroidim.ITQIMObserver;
import tq.com.tqandroidim.TQIM;
import tq.com.tqandroidim.TQIMMsg;
import tq.com.tqandroidim.TQIMStatus;
import tq.com.tqandroidim.TQIMTest;

public class MainActivity extends AppCompatActivity implements ITQIMObserver {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private TextView tvStatus;
    private EditText mEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvStatus = findViewById(R.id.status);
        mEditText = findViewById(R.id.sendInput);

        TQIM.getShareIm().getConfigure().setAutoReconnect(true);
        TQIM.getShareIm().getConfigure().setIp("banqun123.com");
        TQIM.getShareIm().getConfigure().setPort(5300);
        TQIM.getShareIm().addObserver(this);

        findViewById(R.id.startIm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQIM.getShareIm().start();
            }
        });
        findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = mEditText.getText().toString();

                TQIMMsg lTQIMMsg = new TQIMMsg();
                lTQIMMsg.setTo(3);
                lTQIMMsg.setToGroup(0);
                lTQIMMsg.setContent(str);
                lTQIMMsg.setContentType(0);

                TQIM.getShareIm().sendMessage(lTQIMMsg);
            }
        });
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public void onStatuesChange(TQIMStatus old, TQIMStatus n){
        String status = "未连接";
        switch (n){
            case IM_STATUS_CONNECTED: status = "已连接"; break;
            case IM_STATUS_CLOSE: status = "已关闭"; break;
            case IM_STATUS_LOGIN: status = "已登录";break;
            case IM_STATUS_RECONNECTING: status = "连接重试";break;
        }

        tvStatus.setText(status);
    }
    public void onRecvMsg(JSONObject pObject){

    }
}
